/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-05-21 10:55:55
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-06-04 16:03:23
 * @FilePath: \newMould_BHZ\src\components\HntManage\EquipmentCalibration\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/stationProductionPage/HntManage/pouringOrder/WBS/index.vue";

import {
    saveTensioningTask, //保存
    getTensioningTaskByfId, //详情
    getMachineListByStationId, //张拉机
    getTensioningTaskNo, //任务单号
    getStationmachineList, //梁场
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",

    ],
    data() {
        return {
            visible: "",
            loading: false,
            WBSRow: {},
            WBSShow: false,

            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //id
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fTaskno: "", //任务单号
                fBeamid: "", //梁场编号
                fBeamname: "", //梁场名称
                fBeamrealname: "", //梁名称
                fAge: "", //龄期
                fMachineid: "", //设备编号
                fMachinename: "", //设备名称
                fWbsid: "", //工程部位id
                fWbsname: "", //工程部位
                fConcretedesignstrength: "", //混凝土设计强度(张拉) / 孔道类型(压浆)
                fPlantensioningdates: "", //计划张拉日期
                fTensioningforce: "", //设计张拉力
                fBeamtype: "", //梁型
                fBeamnumber: "", //梁号
                fPouringdates: "", //浇筑日期
                fProcessnum: "", //孔道数
                fDesigndensity: "", //设计密度(压浆)
                fTheoreticalslurry: "", //理论浆量(压浆)
                fTaskuserid: "", //提交人id
                fTaskusername: "", //提交人
                fTaskdates: "", //创建时间
                fStatus: "未完成", //状态
                fNote: "2", //1 张拉 2压浆
            },
            formObj: formJs.formObj,
            fSourcefileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,

        };
    },
    components: {
        publicForm,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId,
                            fCompanyid: this.currentDetail.fCompanyid, //标段id
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        this.mixingPlantList(0, this.userInfo.companyId);
        if (this.statusNum == 1) { //新增
            this.getConcrete()
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[4].disabled = true;

            // this.formObj[5].uploadSelect = true;
            this.btnShow = true
        } else if (this.statusNum == 2) { //编辑

            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[4].disabled = true;

            // this.formObj[5].uploadSelect = true;
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            // this.formObj[5].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        refreshList() {},
        WBSDialog() {
            this.WBSShow = false;
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(value, index, prop) {
            console.log(value, index, prop);
            if (index == "fWbsname") {
                this.WBSShow = true
            } else if (prop == "fBeamname") {
                this.formData.fMachinename = "";
                this.formData.fMachineid = "";
                this.formData.fBeamid = value.value;
                this.formData.fBeamname = value.label;
                this.getbhjDataMethods(value.value, 5);
                // this.bhzDataList.map((item) => {
                //     if (item.label == value) {

                //     }
                // });
            }

            if (prop == "fMachinename") {
                this.formData.fMachineid = value.value;
                this.formData.fMachinename = value.label;

                // this.bhjDataList.map((item) => {
                //     if (item.label == value) {
                //         this.formData.fMachineid = item.value;
                //     }
                // });
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        getWBS(data) { //获取材料信息


            this.formData.fWbsname = data.fLongname //施工部位
            this.formData.fWbsid = data.fNodeid //施工部位id

        },
        // 刪除
        delteFile(file) {
            for (let index = 0; index < this.fSourcefileList.length; index++) {
                if (file.uid == this.fSourcefileList[index].uid) {
                    if (this.fSourcefileList[index].atta == undefined) {
                        this.recordfiles.push({
                            fId: this.fSourcefileList[index].fId,
                        });
                    } else {
                        this.recordfiles.push({
                            fId: this.fSourcefileList[index].atta.fId,
                        });
                    }
                    this.fSourcefileList.splice(index, 1);
                }
            }
        },
        // 任务单号
        getConcrete() {
            var data = {
                fCompanyid: this.userInfo.companyId, //标段id
                keyType: "2", ////张拉 1 压浆 2 必填
            };
            getTensioningTaskNo(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loadingEditDialog = false; //弹框loading
                        this.Taskno = res.data.yjQmpMsTensioningtask.fTaskno;
                        this.formData = {
                            fId: "", //id
                            fCompanyid: this.userInfo.companyId, //标段id
                            fCompanyname: this.userInfo.companyName, //标段名称
                            fProjectid: this.userInfo.projectId, //项目id
                            fProjectname: this.userInfo.projectName, //项目名称
                            fTaskno:  this.Taskno, //任务单号
                            fBeamid: "", //梁场编号
                            fBeamname: "", //梁场名称
                            fBeamrealname: "", //梁名称
                            fAge: "", //龄期
                            fMachineid: "", //设备编号
                            fMachinename: "", //设备名称
                            fWbsid: "", //工程部位id
                            fWbsname: "", //工程部位
                            fConcretedesignstrength: "", //混凝土设计强度(张拉) / 孔道类型(压浆)
                            fPlantensioningdates: "", //计划张拉日期
                            fTensioningforce: "", //设计张拉力
                            fBeamtype: "", //梁型
                            fBeamnumber: "", //梁号
                            fPouringdates: "", //浇筑日期
                            fProcessnum: "", //孔道数
                            fDesigndensity: "", //设计密度(压浆)
                            fTheoreticalslurry: "", //理论浆量(压浆)
                            fTaskuserid: this.userInfo.userId, //提交人id
                            fTaskusername: this.userInfo.realName, //提交人
                            fTaskdates: this.common.day(0), //创建时间
                            fStatus: "未完成", //状态
                            fNote: "2", //1 张拉 2压浆
                        };
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getTensioningTaskByfId(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpMsTensioningtask;
                    this.formData = data;
                    if (
                        this.formData.fProjectname == "" ||
                        this.formData.fProjectname == null
                    ) {
                        this.formData.fProjectid = this.userInfo.projectId; //项目id
                        this.formData.fProjectname = this.userInfo.projectName; //项目名称
                    }
                    if (
                        this.formData.fTaskusername == "" ||
                        this.formData.fTaskusername == null
                    ) {
                        this.formData.fTaskuserid = this.userInfo.id; //创建人id
                        this.formData.fTaskusername = this.userInfo.userName; //创建人姓名
                    }
                    if (
                        this.formData.fTaskdates == "" ||
                        this.formData.fTaskdates == null
                    ) {
                        this.formData.fTaskdates = this.timeYmdHms; //创建时间
                    }
                    if (
                        (this.formData.fBeamname == "" ||
                            this.formData.fBeamname == null) &&
                        (this.formData.fBeamid == "" || this.formData.fBeamid == null)
                    ) {
                        this.formData.fMachinename = ""; //设备名称
                        this.formData.fMachineid = ""; //设备编号
                        this.formObjText[3].options = [];
                    } else {
                        this.getbhjDataMethods(this.formData.fBeamid, 5);
                    }
                    // this.formData.fMaintainername = this.userInfo.realName;
                    // this.formData.fMaintainerid = this.userInfo.userId;
                    // if (this.formData.fMaintenancedates == null) {
                    //     this.formData.fMaintenancedates = this.common.day(0);
                    // }

                    // if (this.formData.fCertificatenumber == null) {
                    //     this.formData.fCertificatenumber = "";
                    // }
                    // if (this.formData.fCheckresult == null) {
                    //     this.formData.fCheckresult = "";
                    // }
                    // if (this.formData.fNextcheckdates == null) {
                    //     this.formData.fNextcheckdates = "";
                    // }
                    // this.fSourcefileList = res.data.scanFileList; //证书扫描件
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        // 获取梁场
        mixingPlantList(num, id) {
            var data = {
                companyId: id, // 施工标段id
                modelType: num, // 类型
                stationType: "4", // 拌合站类型
            };

            getStationmachineList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.stationmachineList;
                        this.formObj[2].options = []
                        arr.map((item) => {
                            this.formObj[2].options.push({
                                label: item.fMachinename,
                                value: item.fMachineid,
                            });
                        });

                    } else {
                        this.$message.error(res.msg);

                        return false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 张拉机
        getbhjDataMethods(data, num) {
            this.formObj[3].options = [];
            getMachineListByStationId({
                    stationId: data,
                    modelType: num
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.stationmachineList;
                        arr.map((item) => {
                            this.formObj[3].options.push({
                                label: item.fMachinename,
                                value: item.fMachineid,
                            });
                        });

                    } else {
                        this.$message.error(res.msg);

                        return false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        yj_saveForm(status) {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fProjectname = this.userInfo.projectFullName; //当前登录人id
            this.formData.fProjectid = this.userInfo.projectId; //当前登录人名称
            this.formData.fCompanyname = this.userInfo.companyName; //当前登录人id
            this.formData.fCompanyid = this.userInfo.companyId; //当前登录人名称

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;

            this.isLock = true
            saveTensioningTask(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};